package cn.itcast.usermanage.controller;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.itcast.usermanage.pojo.Search;
import cn.itcast.usermanage.service.SearchService;
import cn.itcast.usermanage.service.VidelService;

@RequestMapping("/api")
@Controller
public class SearchController {
  
    @Autowired
    private SearchService searchService;
    
    @Autowired
    private VidelService VidelService;
    
   /* @RequestMapping(value = "/search" , method = RequestMethod.GET)
    public ResponseEntity<List<Search>> queryByDirection(String direction,String type,int page){        
        try {
           
            List<Search> search = this.searchService.queryByDir(direction);
            if(null == search){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(search);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }*/
    
    /**
     * 查询
     * @param direction
     * @param type
     * @param page
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/search" , method = RequestMethod.GET ,produces="application/json;charset=UTF-8")
    @ResponseBody
    public String query(String direction,String type,Integer page) throws JsonProcessingException{             
        if(page == null){
            page = 1;
           }
        
        if(direction == null && type == null){
            List<Search> list = this.searchService.queryAll();
            List<Search> search = new ArrayList<Search>();
            for(int i=(page-1)*20;i<page*20;i++){
                if(i<=list.size()-1){
                    search.add(list.get(i));
                }                
            }
            Map<String , Object> map = new LinkedHashMap<String , Object>();
            map.put("total", list.size());
            map.put("movie", search);
            ObjectMapper mapper = new ObjectMapper();
            String data = mapper.writeValueAsString(map);
            
            return data;
       
        }else if(type != null){
            Search record = new Search();
            record.setType(type);
            
            List<Search> list = this.searchService.queryByTy(record);
            List<Search> search = new ArrayList<Search>();
            for(int i=(page-1)*20;i<page*20;i++){
                if(i<=list.size()-1){
                    search.add(list.get(i));
                }                
            }
            Map<String , Object> map = new LinkedHashMap<String , Object>();
            map.put("total", list.size());
            map.put("movie", search);
            ObjectMapper mapper = new ObjectMapper();
            String data = mapper.writeValueAsString(map);
            
            return data;
        }else{
            Search record = new Search();
            record.setDirection(direction);
            List<Search> list = this.searchService.queryByDir(record);
            List<Search> search = new ArrayList<Search>();
            for(int i=(page-1)*20;i<page*20;i++){
                if(i<=list.size()-1){
                    search.add(list.get(i));
                }                
            }
            Map<String , Object> map = new LinkedHashMap<String , Object>();
            map.put("total", list.size());
            map.put("movie", search);
            ObjectMapper mapper = new ObjectMapper();
            String data = mapper.writeValueAsString(map);
            System.out.println(direction);
            return data;
        }                 
       
    }
    
    @RequestMapping(value = "/searchid",method = RequestMethod.GET)
    @ResponseBody
    public String queryById(int id) throws JsonProcessingException{
        Search record = new Search();
        record.setId(id);
        Search search = this.searchService.queryone(record);
        Map<String , Object> map = new LinkedHashMap<String , Object>();
        map.put("id", search.getId());
        map.put("images", search.getUrl());
        map.put("title", search.getTitle());
        map.put("summary", search.getSummary());
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.writeValueAsString(map);
        
        return data;
    }
    
    
/*    @RequestMapping(value = "/type" ,method = RequestMethod.GET)
    public ResponseEntity<List<Search>> queryByTypeList(String type){
        
        try {
            List<Search> search = this.searchService.queryByType(type);
            if(null == search){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(search);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }*/
        
    @RequestMapping(value="/count", method = RequestMethod.GET)
    public ResponseEntity<Integer> queryCount(Search record){
        try {
            int search = this.searchService.queryCount(record);
            if(search==0){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(search);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
   
}
